/**
 * Class: Exploder
 *
 * Purpose: 
 *
 * Created: Jan 26, 2010 @ 11:28:32 PM
 * @author Ken Scott
 */
package org.openfantasia.ps.fireworks;

import org.openfantasia.ps.actor.Actor;
import org.openfantasia.ps.actor.SparklingDot;
import org.openfantasia.ps.particle.Particle;
import org.openfantasia.ps.particle.ParticleFactory;
import org.openfantasia.ui.PColor;
import org.openfantasia.util.math.PMath;

public class Exploder extends Particle {

	protected int parts = 100;

	protected double magnitude = 30;

	protected ParticleFactory sparkFactory;

	protected Actor sparkActor;


	public Exploder(int parts, double magnitude, double lifespan) {
		setFillColor(PColor.RED.darker());
		this.parts = parts;
		this.magnitude = magnitude;
		this.lifespan = lifespan;
	}


	public int getParts() {
		return parts;
	}


	public void setParts(int parts) {
		this.parts = parts;
	}


	public double getMagnitude() {
		return magnitude;
	}


	public void setMagnitude(double magnitude) {
		this.magnitude = magnitude;
	}


	public double getLifespan() {
		return lifespan;
	}


	public void setLifespan(double lifespan) {
		this.lifespan = lifespan;
	}


	public void birth(double time) {
		sparkActor = new SparklingDot(getSpace().getFrame());
		sparkFactory = new SparkFactory(getSpace().getFrame(), 1.5, sparkActor);
		super.birth(time);
	}


	public void death(double time) {
		explode();
		super.death(time);
	}


	public void explode() {
		int count = (int) ((double) parts * PMath.random(.75, 1.5));
		for (int i = 0; i < count; i++) {
			Particle spark = sparkFactory.newParticle();
			spark.setPosition(position.copy());
			double mag = magnitude * PMath.random(.95, 1.05);
			spark.setVelocity(velocity.copy().add(velocity.copy().randomize(mag)));
			getSpace().addParticle(spark);
		}
	}
}
